/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.claro.administracionSolicitudes.dao;

import co.com.claro.administracionSolicitudes.dao.exceptions.NonexistentEntityException;
import co.com.claro.administracionSolicitudes.dao.exceptions.RollbackFailureException;
import co.com.claro.administracionSolicitudes.entity.HistoricoParametros;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author Mike
 */
public class HistoricoParametrosJpaController implements Serializable {

    public HistoricoParametrosJpaController() {
    }
    
    @PersistenceContext
    private EntityManager em = null;

    public void create(HistoricoParametros historicoParametros) throws RollbackFailureException, Exception {
        try {
            em.persist(historicoParametros);
        } catch (Exception ex) {
            try {
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } 
    }

    public void edit(HistoricoParametros historicoParametros) throws NonexistentEntityException, RollbackFailureException, Exception {
        try {
            historicoParametros = em.merge(historicoParametros);
        } catch (Exception ex) {
            try {
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = historicoParametros.getId();
                if (findHistoricoParametros(id) == null) {
                    throw new NonexistentEntityException("The historicoParametros with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } 
    }

    public void destroy(Long id) throws NonexistentEntityException, RollbackFailureException, Exception {
        try {
            HistoricoParametros historicoParametros;
            try {
                historicoParametros = em.getReference(HistoricoParametros.class, id);
                historicoParametros.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The historicoParametros with id " + id + " no longer exists.", enfe);
            }
            em.remove(historicoParametros);
        } catch (Exception ex) {
            try {
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        }
    }

    public List<HistoricoParametros> findHistoricoParametrosEntities() {
        return findHistoricoParametrosEntities(true, -1, -1);
    }

    public List<HistoricoParametros> findHistoricoParametrosEntities(int maxResults, int firstResult) {
        return findHistoricoParametrosEntities(false, maxResults, firstResult);
    }

    private List<HistoricoParametros> findHistoricoParametrosEntities(boolean all, int maxResults, int firstResult) {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(HistoricoParametros.class));
        Query q = em.createQuery(cq);
        if (!all) {
            q.setMaxResults(maxResults);
            q.setFirstResult(firstResult);
        }
        return q.getResultList();
    }

    public HistoricoParametros findHistoricoParametros(Long id) {
        try {
            return em.find(HistoricoParametros.class, id);
        } finally {
            em.close();
        }
    }

    public int getHistoricoParametrosCount() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        Root<HistoricoParametros> rt = cq.from(HistoricoParametros.class);
        cq.select(em.getCriteriaBuilder().count(rt));
        Query q = em.createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }
    
}
